# the minimum version of CMake.
cmake_minimum_required(VERSION 3.5.0)
project(ndk)

set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR})
set(SENTRY_NATIVE_SRC ${CMAKE_SOURCE_DIR}/../../../../../sentry-native)
# flutter/ohos/src/main/cpp/CMakeLists.txt
# flutter/sentry-native
if (NOT THIRDPARTY_DIR)
	set(THIRDPARTY_DIR "${SENTRY_NATIVE_SRC}/external/external/third_party")
endif()

# Adding sentry-native project
add_subdirectory(${SENTRY_NATIVE_SRC} sentry_build)

if(DEFINED PACKAGE_FIND_FILE)
    include(${PACKAGE_FIND_FILE})
endif()

include_directories(${NATIVERENDER_ROOT_PATH}
                    ${NATIVERENDER_ROOT_PATH}/include)

add_library(entry SHARED ndk_sample.cpp)
target_link_libraries(entry PUBLIC libace_napi.z.so $<BUILD_INTERFACE:sentry::sentry>)

# Build replay_encoder directly here so the target is part of entry build
set(REPLAY_ENCODER_SRC_DIR ${CMAKE_SOURCE_DIR}/../../../../../../ohos/src/lib/sentry_ohos/src/main/cpp/replay_encoder)
add_library(replay_encoder SHARED
    ${REPLAY_ENCODER_SRC_DIR}/NapiBridge.cpp
    ${REPLAY_ENCODER_SRC_DIR}/ReplayEncoder.cpp
)
set_target_properties(replay_encoder PROPERTIES OUTPUT_NAME "replay_encoder")
target_include_directories(replay_encoder PRIVATE ${REPLAY_ENCODER_SRC_DIR})
# Prefer explicit .so names available in intermediates
target_link_libraries(replay_encoder PRIVATE libace_napi.z.so avcodec avmuxer)
target_link_libraries(entry PUBLIC replay_encoder)
add_dependencies(entry replay_encoder)

# Copy built so into src/main/libs for packaging
add_custom_command(TARGET replay_encoder POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_SOURCE_DIR}/../libs/arm64-v8a
    COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:replay_encoder> ${CMAKE_CURRENT_SOURCE_DIR}/../libs/arm64-v8a/libreplay_encoder.so
    COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_SOURCE_DIR}/../libs/arm64-v8a
    COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:replay_encoder> ${CMAKE_SOURCE_DIR}/../libs/arm64-v8a/libreplay_encoder.so
)

